﻿using Furion;
using Furion.Logging.Extensions;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;

namespace ZeroAdmin.Core;

/// <summary>
/// SqlSuagr启动设置
/// </summary>
public static class SqlSugarSetup
{

    /// <summary>
    /// SqlsugarScope的配置
    /// Scope必须用单例注入
    /// 不可以用Action委托注入
    /// </summary>
    /// <param name="services"></param>
    public static void SqlSugarScopeConfigure(this IServiceCollection services)
    {
        //数据库序号从0开始,默认数据库为0
        var config = App.GetConfig<DatabaseConfig>("ConnectionStrings", true);

        ConnectionConfig connectConfig =
            new()
            {
                ConnectionString = config.DefaultDbString,
                DbType = (DbType)Convert.ToInt32(Enum.Parse(typeof(DbType), config.DefaultDbType)),
                IsAutoCloseConnection = true,
                ConfigId = 1,
                InitKeyType = InitKeyType.Attribute,
                MoreSettings = new ConnMoreSettings()
                {
                    IsAutoRemoveDataCache = true//自动清理缓存
                }
            };

        SqlSugarScope sqlSugarScope = new(connectConfig,
            //全局上下文生效
            db =>
            {
                //执行超时时间
                db.Ado.CommandTimeOut = 30;

                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    if (sql.StartsWith("SELECT"))
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                    }
                    if (sql.StartsWith("UPDATE") || sql.StartsWith("INSERT"))
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    if (sql.StartsWith("DELETE"))
                    {
                        Console.ForegroundColor = ConsoleColor.Blue;
                    }
                    App.PrintToMiniProfiler("SqlSugar", "Info", UtilMethods.GetSqlString(connectConfig.DbType, sql, pars));
                    $"DB: Sql:\r\n\r\n {UtilMethods.GetSqlString(connectConfig.DbType, sql, pars)}".LogInformation();
                };

                db.Aop.DataExecuting = (oldValue, entityInfo) =>
                {
                    if (entityInfo.OperationType == DataFilterType.InsertByObject)
                    {
                        if (entityInfo.PropertyName == DatabaseConst.CreateTime)
                            entityInfo.SetValue(DateTime.Now);
                        if (entityInfo.PropertyName == DatabaseConst.UpdateTime)
                            entityInfo.SetValue(DateTime.Now);
                    }
                    else if (entityInfo.OperationType == DataFilterType.UpdateByObject)
                    {
                        if (entityInfo.PropertyName == DatabaseConst.UpdateTime)
                            entityInfo.SetValue(DateTime.Now);
                    }
                    else if (entityInfo.OperationType == DataFilterType.DeleteByObject)
                    {
                    }
                };
            });
        services.AddSingleton<ISqlSugarClient>(sqlSugarScope);
    }
}
